from dataclasses import dataclass


@dataclass
class SSHConfig:
    hostname: str
    username: str
    port: str | int = 22
    private_key: str | None = None
    private_key_filepath: str | None = None

    @property
    def url(self) -> str:
        if self.port == 22:
            return f"ssh://{self.username}@{self.hostname}"
        else:
            return f"ssh://{self.username}@{self.hostname}:{self.port}"

    @url.setter
    def url(self, url: str):
        if url.startswith("ssh://"):
            username_hostname_port = url[6:]
        else:
            username_hostname_port = url

        username_hostname_port = username_hostname_port.split('@')
        if len(username_hostname_port) != 2:
            raise ValueError(f"Invalid url: {url}")
        self.username, hostname_and_port = username_hostname_port

        hostname_and_port = hostname_and_port.split(':')
        if len(hostname_and_port) == 1:
            self.hostname = hostname_and_port[0]
            self.port = 22
        elif len(hostname_and_port) == 2:
            self.hostname, port = hostname_and_port
            self.port = int(port)
        else:
            raise ValueError(f"Invalid url: {url}")
